import { Toast } from "vant"
import dayjs from "dayjs";
import 'vant/lib/index.less';

export const openLoading = () => {
  Toast.loading({
    duration: 0,
    message: '加载中...',
    forbidClick: true,
  })
}

export const closeLoading = () => {
  Toast.clear()
}

export const toast = (msg, position = 'bottom') => {
  Toast.fail({
    position: position,
    message: msg,
  })
  //Toast.resetDefaultOptions();
  Toast.setDefaultOptions({ duration: 4000 });
}

/* export const toastFail = (msg) => {
  Toast.fail(msg);
} */

export const parseQRCode = (code) => {
  console.log('1');
  if (code.match(/([A-Za-z0-9\d\s\t\n]+)[|]/)) {
    console.log('2');
    let palletNoReg = code.match(/([A-Za-z0-9\d\s\t\n]+)[|]/)
    let materialReg = code.match(/(?<=[|]).*?(?=[|])/g)[0];
    let quantityReg = code.match(/(?<=[|]).*?(?=[|])/g)[5];
    let ProdDateReg = code.match(/(?<=[|]).*?(?=[|])/g)[1];
    if (materialReg && quantityReg && palletNoReg) {
      return {
        materialCode: materialReg,
        quantity: Number(quantityReg),
        prodDate: ProdDateReg,
        palletNo: palletNoReg[1],
      }
    }
    return null
  } else if (code.substring(0, 1) == '(') {
    if (code.match(/[(]0[)](\d+)/)) {
      console.log('3');
      let materialReg = code.match(/[(]241[)](\d+)[(]/)
      let quantityReg = code.match(/[(]30[)]([0-9.]+)[(]/)
      let ProdDateReg = code.match(/[(]11[)]([A-Za-z0-9-]+)[(]/)
      let palletNoReg = code.match(/[(]94[)]([A-Za-z0-9]+)[(]/)
      let SerialNoReg = code.match(/[(]0[)]([0-9]+)/)

      if (materialReg && quantityReg && palletNoReg) {
        return {
          materialCode: materialReg[1],
          quantity: Number(quantityReg[1]),
          prodDate: ProdDateReg[1],
          palletNo: palletNoReg[1],
          SerialNo: SerialNoReg[1]
        }
      }
      return null
    } else {
      console.log('4');
      let materialReg = code.match(/[(]241[)](\d+)[(]/)
      let quantityReg = code.match(/[(]30[)]([0-9.]+)[(]/)
      let ProdDateReg = code.match(/[(]11[)]([A-Za-z0-9-]+)[(]/)
      let palletNoReg = code.match(/[(]94[)]([A-Za-z0-9]+)[(]/)

      if (materialReg && quantityReg && palletNoReg) {
        return {
          materialCode: materialReg[1],
          quantity: Number(quantityReg[1]),
          prodDate: ProdDateReg[1],
          palletNo: palletNoReg[1],
        }
      }
      return null
    }
  } else {
    return null
  }
}

export const dateToString = (date, tmp = 'YYYY-MM-DD HH:mm') => {
  return dayjs(date).format(tmp)
}
